package com.ll.beautymall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ll.beautymall.R;
import com.ll.beautymall.bean.ObjectProduct;
import com.ll.beautymall.ui.home.taobaoproduct.TaoBaoProductActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/26.
 */
public class ZeroAdapter extends RecyclerView.Adapter<ZeroAdapter.ViewHolder> {
    private List<ObjectProduct> objectProducts;
    private Context mContext;

    public ZeroAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<ObjectProduct> list) {
        this.objectProducts = list;
        notifyDataSetChanged();
    }

    public void addData(List<ObjectProduct> list) {
        this.objectProducts.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_zero_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ObjectProduct product = objectProducts.get(position);
        holder.homeGvItemTvOldPrice.setText(product.getTaobao_price());
        holder.homeGvItemTvOldPrice.setPaintFlags(holder.homeGvItemTvOldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

        holder.homeGvItemTvDiscountPrice.setText(product.getTaobao_selling_price());
        holder.homeGvItemTvSale.setText(product.getDiscount());
        holder.homeGvItemTvTitle.setText(product.getTaobao_title());
        Picasso.with(mContext).load(product.getTaobao_pic_url()).into(holder.homeGvItemIvCover);

        holder.homeGvItemIvCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, TaoBaoProductActivity.class);
                ObjectProduct objectProduct = objectProducts.get(position);
                intent.putExtra("product", objectProduct);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return objectProducts != null ? objectProducts.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_gv_item_iv_cover)
        ImageView homeGvItemIvCover;
        @BindView(R.id.home_gv_item_tv_title)
        TextView homeGvItemTvTitle;
        @BindView(R.id.home_gv_item_tv_discount_price)
        TextView homeGvItemTvDiscountPrice;
        @BindView(R.id.home_gv_item_tv_old_price)
        TextView homeGvItemTvOldPrice;
        @BindView(R.id.home_gv_item_tv_sale)
        TextView homeGvItemTvSale;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
